package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.GroupAllMembersAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.GroupDetailsBean;
import com.beiing.tianshuai.tianshuai.mine.ui.PersonalDataCardActivity;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class GroupAllMembersActivity extends BaseActivity {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.rv_all_group_members)
    RecyclerView mRvAllGroupMembers;

    private static final String                                     TAG = "GroupAllMembersActivity";
    private Unbinder                                                mUnbinder;
    private Context                                                 mContext;
    private List<GroupDetailsBean.DataBean.NumberBean.UserBean>     mMembers;
    private GroupAllMembersAdapter                                  mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_group_all_members;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        initToolbar();
        initData();
        initRecyclerView();
        initListener();
    }

    private void initToolbar() {
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarTvTitle.setText("群聊成员");
    }

    private void initData() {
        Intent intent = getIntent();
        mMembers = ((List<GroupDetailsBean.DataBean.NumberBean.UserBean>)
                intent.getSerializableExtra("members"));
        LogUtils.i(mMembers.toString(), TAG);
    }

    private void initListener() {
        //条目点击
        mAdapter.setListener(new GroupAllMembersAdapter.onItemClick() {
            @Override
            public void OnItemClickListener(int position, GroupDetailsBean.DataBean.NumberBean.UserBean bean) {
                Intent intent = new Intent(mContext, PersonalDataCardActivity.class);
                intent.putExtra("uid", bean.getUid());
                startActivity(intent);
            }
        });

        //返回
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initRecyclerView() {
        mAdapter = new GroupAllMembersAdapter(mContext);
        mAdapter.appendData(mMembers);
        mRvAllGroupMembers.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRvAllGroupMembers.setNestedScrollingEnabled(false);
        mRvAllGroupMembers.setLayoutManager(linearLayoutManager);
        //绘制item间的分割线，
        mRvAllGroupMembers.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, 1, ContextCompat.getColor(mContext, R.color.divider_gray_light_efefef)));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }
}
